package router

import (
	"fmt"
	"service/components"
	"service/controller"
	"service/middleware"
	"service/modules"
	"service/utils"

	"github.com/gin-gonic/gin"
)

func UserRouter(userRouter *gin.RouterGroup) {

	userRouter.GET("", middleware.AuthVerifyMiddleware, controller.GetAllUserController)

	userRouter.GET("/search", middleware.AuthVerifyMiddleware, controller.GetUserInfoDetailController)

	userRouter.GET("/:id", middleware.AuthVerifyMiddleware, controller.GetUserInfoController)

	userRouter.PATCH("", middleware.AuthVerifyMiddleware, controller.UpdatedUserInfoController)

	userRouter.POST("/send-verify-code", controller.SendCodeService)

	userRouter.PATCH("/reset-pass", controller.ResetPasswordController)

	userRouter.DELETE("/:id", middleware.AuthVerifyMiddleware, controller.DeleteUserByIdController)

	userRouter.POST("",
		middleware.AuthVerifyMiddleware,
		middleware.UserRegisterVerifyMiddleware,
		controller.RegisterController)
	userRouter.POST("/lhj",
		middleware.UserRegisterVerifyMiddleware,
		controller.RegisterController)

	userRouter.POST("/login", middleware.UserLoginVerifyMiddleware, controller.LoginController)

	// 退出登录
	userRouter.POST("/logout", middleware.AuthVerifyMiddleware, func(context *gin.Context) {
		user := context.MustGet("user").(*modules.Users)
		myUser := &modules.Users{}
		err := components.DB.
			Model(&modules.Users{}).
			Where("id = ?", user.Id).
			First(&myUser).
			Updates(map[string]interface{}{
				"is_login": false,
			}).Error
		if err != nil {
			fmt.Println("err  pch", err.Error())
		}
		// utils.Logout(&utils.UserClaims{
		// 	UserName: user.UserName,
		// })
		utils.HandleSuccessResponse(context, "退出登录成功!", nil)
	})

	userRouter.POST("/read", middleware.AuthVerifyMiddleware, func(ctx *gin.Context) {
		user := ctx.MustGet("user").(*modules.Users)
		data := map[string]interface{}{}
		ctx.ShouldBind(&data)
		if user.UserName != data["user_name"] {
			_, err := modules.FindUserByNameService(data["user_name"].(string))
			if err == nil {
				utils.HandleFailResponse(ctx, data["user_name"].(string)+" 该用户已存在", data)
				return
			}
		}
		if user.Phone != data["phone"] {
			_, err := modules.FindUserByPhoneService(data["phone"].(string))
			if err == nil {
				utils.HandleFailResponse(ctx, data["phone"].(string)+" 该手机号码已存在", data)
				return
			}
		}
		if user.Email != data["email"] {
			_, err := modules.FindUserByEmailService(data["email"].(string))
			if err == nil {
				utils.HandleFailResponse(ctx, data["email"].(string)+" 该邮箱已存在", data)
				return
			}
		}
		if user.OrderPrefix != data["order_prefix"] {
			user, err := modules.FindUserByOrderPrefixService(data["order_prefix"].(string))
			if err == nil {
				utils.HandleFailResponse(ctx, data["order_prefix"].(string)+" 该订单前缀已被： "+user.UserName+" 使用", data)
				return
			}
		}
		fmt.Println("=> ", data["user_name"], user.UserName)
		hashPwd, err := utils.HandlePwdEncryptionMd5(data["password"].(string))
		if err != nil {
			utils.HandleFailResponse(ctx, "用户密码更新失败", nil)
			return
		}
		data["password"] = hashPwd
		data["read_account"] = true
		data["edit_account"] = false
		err = components.DB.Model(&modules.Users{}).
			Where("id = ?", user.Id).
			Updates(data).Error
		if err != nil {
			utils.HandleFailResponse(ctx, "更新用户失败", data)
			return
		}
		utils.HandleSuccessResponse(ctx, "帐号完成!", data)
	})
}
